package heman.server.controllers;

import heman.server.entities.TableManager;



import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.server.interfaces.IProcessServerMessage;

import org.w3c.dom.Document;


/**
 * Table controller handles the request of "tables".
 * 
 * 
 * 
 * <tableResponse>
- <table id="13" seed="12944" type="public" full="false" moderator="1213" game="WordSteal" options="turnTime=30,noS=true,maxScore=50">
  <player-id player="11" /> 
  <player-id player="1213" /> 
  </table>
- <table id="17" seed="11144" type="private" full="true" moderator="18" game="solitaire/freeCell" options="undo=true,newHand=true,time=500">
  <player-id player="18" /> 
  <player-id player="17" /> 
  </table>
  </tableResponse>

 * 
 * @author Wenlong Liu
 *
 */
public class TablesController implements IProcessServerMessage {

	public boolean process(Communicator com, Message m) {
		String originator = m.getOriginator();
        
        // produce successful request. note that client is responsible 
        // now for tagging from.
        StringBuilder sb = new StringBuilder(Message.responseHeader(true,m.id));
        //sb.append("")
        sb.append("<tableResponse>");
        
        sb.append(TableManager.getInstance().toXML());
        sb.append("</tableResponse></response>");
        //System.out.println(sb.toString());
        Document d = Message.construct(sb.toString());
        
        Message r = new Message (d);
        r.setOriginator(originator);
        r.setRecipient(originator);
        return com.distribute(r);
       
	}

}
